﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NearForums.DataAccess.Xml
{
    class TagsDataAccess : BaseDataAccess, ITagsDataAccess
    {
        public List<WeightTag> GetMostViewed(int forumId, int top)
        {
            XDocument doc = XDocument.Load(GetDataPath(TagsFile));
            var qry = from t in doc.Root.Elements("Tag")
                      select new
                      {
                          Tag = (string)t.Attribute("Name"),
                          TopicId = (int)t.Attribute("TopicId"),
                          Views = 0
                      };

            TopicsDataAccess tda = new TopicsDataAccess();
            var tagViews = new Dictionary<string, int>();
            foreach (var item in qry)
            {
                var views = tda.Get(item.TopicId).Views;
                if (views == 0)
                    views = 1;

                if (tagViews.ContainsKey(item.Tag))
                    tagViews[item.Tag] = tagViews[item.Tag] + views;
                else
                    tagViews[item.Tag] = views;
            }

            var total = tagViews.Sum(kvp => kvp.Value);
            var weightTags = new List<WeightTag>();
            foreach (var key in tagViews.Keys)
            {
                var weight = (decimal)tagViews[key] / total * 100;
                var weightTag = new WeightTag(key, weight);
                weightTags.Add(weightTag);
            }

            return weightTags.OrderByDescending(wt => wt.Weight).Take(top).ToList();

            //var qry = from t in doc.Root.Elements("Tag")
            //          group t by (string)t.Attribute("Name")
            //              into tagGroup
            //              select new
            //              {
            //                  Name = tagGroup.Key,
            //                  Count = tagGroup.Count()
            //              };

            //int total = qry.Sum(a => a.Count);
            //List<WeightTag> tags = new List<WeightTag>();
            //foreach (var item in qry)
            //{
            //    WeightTag tag = new WeightTag(item.Name, 0);
            //    tag.Weight = ((decimal)item.Count / total) * 100;
            //    tags.Add(tag);
            //}

            //return tags.OrderByDescending(w => w.Weight)
            //           .Take(top)
            //           .ToList();       
        }

        internal void Add(string tag, int topicId)
        {
            string filePath = GetDataPath(TagsFile);
            XDocument doc = XDocument.Load(filePath);

            XElement elem = new XElement("Tag",
                                new XAttribute("Name", tag),
                                new XAttribute("TopicId", topicId)
                                );

            doc.Root.Add(elem);
            doc.Save(filePath);
        }

        internal void RemoveByTopic(int topicId)
        {
            string filePath = GetDataPath(TagsFile);
            XDocument doc = XDocument.Load(filePath);

            var qry = from t in doc.Root.Elements("Tag")
                      where (int)t.Attribute("TopicId") == topicId
                      select t;

            qry.Remove();

            doc.Save(filePath);
        }

        internal List<int> GetTopics(string tag)
        {
            XDocument doc = XDocument.Load(GetDataPath(TagsFile));
            var qry = from t in doc.Root.Elements("Tag")
                      where (string)t.Attribute("Name") == tag
                      select (int)t.Attribute("TopicId");
            return qry.ToList();
        }
    }
}
